package xdu.lz.stage5_jucUtils.chapter2_cycliBarrier;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * Created at 2019/10/18 0018 下午 5:19
 */
public class CB_test1 {

    public static void main(String[] args) throws BrokenBarrierException, InterruptedException {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
        CyclicBarrier cyclicBarrier1 = new CyclicBarrier(2, new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("recall back : all the work finished");
            }
        });
        new Thread(()->{
            try {
                Thread.sleep(1000);
                System.out.println("T1 finished");
                cyclicBarrier.await();
                cyclicBarrier1.await();
                System.out.println("T1 : get the notice:the other thread finish too.");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(()->{
            try {
                Thread.sleep(3000);
                System.out.println("T2 finished");
                cyclicBarrier.await();
                cyclicBarrier1.await();
                System.out.println("T2 : get the notice:the other thread finish too.");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();


        cyclicBarrier.await();
        Thread.sleep(10);
        System.out.println("all the work finished");

        cyclicBarrier.getNumberWaiting();
    }
}
